- Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathall sorting methods.py
176 lines (135 loc) · 4.35 KB
/
all sorting methods.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# BUBBLE SORT
defbubblesort(customList):
foriinrange(len(customList)-1): #O(n)
forjinrange(len(customList)-i-1): #O(n)
ifcustomList[j]>customList[j+1]: #O(1)
customList[j],customList[j+1]=customList[j+1],customList[j]
print(customList)
# cList=[2,1,3,6,9,7,4,8,5]
# bubblesort(cList)
# SELECTION SORT
defselectionSort(customList):
foriinrange(len(customList)):
min_index=i
forjinrange(i+1,len(customList)):
ifcustomList[min_index] >customList[j]:
min_index=j
customList[i],customList[min_index] =customList[min_index],customList[i]
print(customList)
# cList=[2,1,3,6,9,7,4,8,5]
# selectionSort(cList)
# INSERTION SORT
definsertionSort(customList):
foriinrange(1,len(customList)):
key=customList[i]
j=i-1
whilej>=0andkey<customList[j]:
customList[j+1] =customList[j]
j-=1
customList[j+1] =key
returncustomList
# cList=[2,1,3,6,9,7,4,8,5]
# print(insertionSort(cList))
# BUCKET SORT
importmath
fromxmlrpc.serverimportMultiPathXMLRPCServer
defbucketSort(customList):
numberofBuckets=round(math.sqrt(len(customList)))
maxValue=max(customList)
arr=[]
foriinrange(numberofBuckets):
arr.append([])
forjincustomList:
index_b=math.ceil(j*numberofBuckets/maxValue)
arr[index_b-1].append(j)
foriinrange(numberofBuckets):
arr[i] =insertionSort(arr[i])
k=0
foriinrange(numberofBuckets):
forjinrange(len(arr[i])):
customList[k] =arr[i][j]
k+=1
returncustomList
# cList=[2,1,3,6,9,7,4,8,5]
# print(bucketSort(cList))
# MERGE SORT ALGORITHM
defmerge(customList, l, m, r):
n1=m-l+1
n2=r-m
L=[0]*(n1)
R=[0]*(n2)
foriinrange(0,n1):
L[i] =customList[l+i]
forjinrange(0,n2):
R[j] =customList[m+1+j]
i=0
j=0
k=l
whilei<n1andj<n2:
ifL[i] <=R[j]:
customList[k] =L[i]
i+=1
else:
customList[k] =R[j]
j+=1
k+=1
whilei<n1:
customList[k] =L[i]
i+=1
k+=1
whilej<n2:
customList[k] =R[j]
j+=1
k+=1
# TIME COMPLEXITY : O(N)
defmergeSort(customList, l, r):
ifl<r:
m= (l+(r-1))//2
mergeSort(customList, l, m) #T(n/2)
mergeSort(customList, m+1, r) #T(n/2)
merge(customList, l, m, r)
returncustomList
# cList=[2,1,3,6,9,7,4,8,5]
# print(mergeSort(cList ,0 ,8))
# QUICK SORT
defpartition(customList,low,high):
i=low-1
pivot=customList[high]
forjinrange(low,high):
ifcustomList[j] <=pivot:
i+=1
customList[i],customList[j] =customList[j],customList[i]
customList[i+1],customList[high] =customList[high] , customList[i+1]
return (i+1)
defquickSort(customList ,low ,high):
iflow<high:
pi=partition(customList ,low ,high) #O(n)
quickSort(customList ,low ,pi-1) #T(n/2)
quickSort(customList ,pi+1 ,high) #T(n/2)
#combined = O(N LogN)
# cList=[2,1,3,6,9,7,4,8,5]
# quickSort(cList ,0 ,8)
# print(cList)
# HEAP SORT ALGORITHM
defheapify(customList,n,i):
smallest=i
l=2*i+1
r=2*i+2
ifl<nandcustomList[l] <customList[smallest]:
smallest=l
ifr<nandcustomList[r] <customList[smallest]:
smallest=r
ifsmallest!=i:
customList[i], customList[smallest] =customList[smallest], customList[i]
heapify(customList ,n ,smallest)
defheapSort(customList):
n=len(customList)
foriinrange(int(n/2)-1,-1,-1):
heapify(customList,n,i)
foriinrange(n-1,0,-1):
customList[i],customList[0]=customList[0],customList[i]
heapify(customList,i,0)
customList.reverse()
cList=[2,1,3,6,9,7,4,8,5]
heapSort(cList)
print(cList)